/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.DeviceGroupDetailModel;
import org.testis.gui.mvc.view.testbed.DeviceGroupDetailView;
import org.testis.orm.testbed.refined.DeviceGroupDetailRefined;
import org.testis.orm.testbed.refined.DeviceGroupRefined;
import org.testis.orm.testbed.refined.OperatingSystemRefined;
import org.testis.orm.testbed.refined.TestBedItem;
import org.testis.orm.testbed.refined.TestPcRefined;
import org.testis.orm.testbed.datahandlers.DeviceGroupDetailDataHandler;
import org.testis.orm.testbed.refined.DeviceGroupDetailFactory;
import org.testis.orm.testbed.refined.DeviceGroupFactory;
import org.testis.utils.tree.TbedMutableTreeNode;
import java.util.HashMap;

/**
 *
 * @author selcuk
 */
public class DeviceGroupDetailController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String DETAIL_REFINED_PROPERTY = "DetailRefined";
    public static final String DETAIL_ID_PROPERTY = "DetailId";
    public static final String GROUPID_PROPERTY = "DeviceGroupId";
    public static final String PCID_PROPERTY = "PcId";
    public static final String MACADDRESS_PROPERTY = "MacAddress";
    public static final String IP_PROPERTY = "Ip";
    public static final String PORT_PROPERTY = "Port";
    public static final String OSID_PROPERTY = "OsId";
    public static final String ISREMOTE_PROPERTY = "IsRemote";
    public static final String NAME_PROPERTY = "Name";
    private TbedMutableTreeNode m_selectedNode = null;

    public DeviceGroupDetailController() {
        super();
    }

    private void initialize(boolean editMode) {
        DeviceGroupDetailModel model = new DeviceGroupDetailModel();
        DeviceGroupDetailView view = new DeviceGroupDetailView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationNewDeviceGroupDetail() {
        initialize(false);

        TestBedItem _testBedItem =
                TestBedControllerList.newInstance().getTbedTreeController().getSelectedTestBedItem();
        if (_testBedItem != null && _testBedItem instanceof DeviceGroupRefined) {
            DeviceGroupRefined _group = (DeviceGroupRefined) _testBedItem;
            setModelProperty(GROUPID_PROPERTY, _group.getId());
        }

        setModelProperty(VISIBLE_PROPERTY, true);
    }

    public void operationEditDeviceGroupDetail(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof DeviceGroupDetailRefined) {
            m_selectedNode = node;
            DeviceGroupDetailRefined detail = getSelectedDeviceGroupDetail();
            if (detail == null) {
                System.out.println(
                        "Not found design part for DeviceGroupDetailRefined");
                return;
            }

            initialize(true);
            setModelProperty(DETAIL_REFINED_PROPERTY, detail);
            setModelProperty(DETAIL_ID_PROPERTY, detail.getId());
            DeviceGroupRefined deviceGroup = detail.getDeviceGroup();
            if (deviceGroup != null) {
                setModelProperty(GROUPID_PROPERTY, deviceGroup.getId());
            }
            TestPcRefined pc = detail.getPc();
            if (pc != null) {
                setModelProperty(PCID_PROPERTY, pc.getId());
            }
            OperatingSystemRefined os = detail.getOs();
            if (os != null) {
                setModelProperty(OSID_PROPERTY, os.getId());
            }
            setModelProperty(MACADDRESS_PROPERTY, detail.getMacAddress());
            setModelProperty(IP_PROPERTY, detail.getIpAddress());
            setModelProperty(PORT_PROPERTY, detail.getPort());
            setModelProperty(ISREMOTE_PROPERTY, detail.isRemote());
            setModelProperty(NAME_PROPERTY, detail.getName());
            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    private DeviceGroupDetailRefined getSelectedDeviceGroupDetail() {
        DeviceGroupDetailRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof DeviceGroupDetailRefined) {
            result = (DeviceGroupDetailRefined) obj;
        }
        return result;
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationSaveDetail(DeviceGroupDetailRefined refined,
            HashMap values) {
        Integer detailId = (Integer) values.get("detailId");
        String name = (String) values.get("name");
        String ip = (String) values.get("ip");
        String mac = (String) values.get("mac");
        Boolean isRemote = (Boolean) values.get("isRemote");
        Integer port = (Integer) values.get("port");
        Integer groupId = (Integer) values.get("groupId");
        Integer pcId = (Integer) values.get("pcId");
        Integer osId = (Integer) values.get("osId");

        DeviceGroupRefined groupRefined = DeviceGroupFactory.newInstance().getDeviceGroup(
                groupId);
        if (groupRefined != null) {
            DeviceGroupDetailRefined detailRefined = groupRefined.getDetailByPcId(
                    pcId);
            if (detailRefined != null && detailId != detailRefined.getId()) {
                setModelProperty(ERROR_PROPERTY,
                        detailRefined.getPc().getName() + " already defined in group");
                return;
            }
        }


        boolean move = (refined.getDeviceGroup().getId() != groupId);
        if (DeviceGroupDetailDataHandler.newInstance().saveDeviceGroupDetail(
                detailId, name, ip, mac, isRemote, port, groupId, pcId, osId)) {//move node from one to another
            if (move) {
                TestBedControllerList.newInstance().getTbedTreeController().operationMoveSelectedTestBedItem(
                        groupId);
            }
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to edit DeviceGroupDetail record");
        }
    }

    public void operationAddDetail(String name, String ip, String mac,
            boolean isRemote, int port, int groupId, int pcId, int osId) {
        DeviceGroupRefined groupRefined = DeviceGroupFactory.newInstance().getDeviceGroup(
                groupId);
        if (groupRefined != null) {
            DeviceGroupDetailRefined detailRefined = groupRefined.getDetailByPcId(
                    pcId);
            if (detailRefined != null) {
                setModelProperty(ERROR_PROPERTY,
                        detailRefined.getPc().getName() + " already defined in group");
                return;
            }
        }

        DeviceGroupDetailRefined refined = DeviceGroupDetailFactory.newInstance().createDetail(
                name, ip, mac, isRemote, port, groupId, pcId, osId);
        if (refined != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    refined);
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to add DeviceGroupDetail record");
        }
    }

    public void operationDeleteDeviceGroupDetail(
            DeviceGroupDetailRefined refined) {
        if (DeviceGroupDetailFactory.newInstance().deleteDetail(refined)) {
            TestBedControllerList.newInstance().getTbedTreeController().operationRemoveSelectedTestBedItem();
        } else {
            TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                    "Unable to remove DeviceGroupDetail record");
        }
    }

    public void operationDeleteSelectedDeviceGroupDetail() {
        TestBedItem tbedItem = TestBedControllerList.newInstance().getTbedTreeController().getSelectedTestBedItem();
        if (tbedItem != null && tbedItem instanceof DeviceGroupDetailRefined) {
            DeviceGroupDetailRefined refined = (DeviceGroupDetailRefined) tbedItem;
            operationDeleteDeviceGroupDetail(refined);
        }
    }
}
